class EventBus {
    constructor() {
      this.eventBus = {}
    }
  
    on(eventName: string, eventCallback: Function, thisArg: any) {
      const handles = this.eventBus[eventName]
      if (!handles) this.eventBus[eventName] = []
      this.eventBus[eventName].push({
        eventCallback,
        thisArg
      })
    }
  
    emit(eventName: string, ...payload: any[]) {
      const handles = this.eventBus[eventName]
      if (!handles) return
      for (let { eventCallback, thisArg } of handles) {
        eventCallback.apply(thisArg, payload)
      }
    }
  
    off(eventName: string, eventCallback: Function) {
      const handles = this.eventBus[eventName]
      if (!handles) return
      const deleteIndex = handles.findIndex((handle) => {
        return handle.eventCallback === eventCallback
      })
      if (deleteIndex > -1) {
        this.eventBus[eventName].splice(deleteIndex, 1)
      }
    }
  }
  
  export default new EventBus()
  